# 使用官方 Python 镜像
FROM python:3.11-slim AS builder
# 设置环境变量
ENV PYTHONUNBUFFERED=1
ENV DEBIAN_FRONTEND=noninteractive
# 设置工作目录
WORKDIR /app
# 安装系统依赖和 Rust 工具链
RUN apt-get update -y && \
    apt-get install -y --no-install-recommends \
    build-essential \
    gcc \
    git \
    wget \
    ca-certificates \
    curl \
    && \
    # 安装 Rust
    curl https://sh.rustup.rs -sSf | sh -s -- -y && \
    # 清理 apt 缓存
    rm -rf /var/lib/apt/lists/*
# 设置 Rust 环境变量
ENV PATH="/root/.cargo/bin:${PATH}"
# 配置 pip 使用官方源，并设置超时
RUN pip config set global.timeout 100
# 升级 pip
RUN pip install --upgrade pip
# 复制所有项目文件
COPY . .
# 安装依赖（使用官方源）
RUN pip install --default-timeout=100 \
    --no-cache-dir \
    -r requirements.txt
# 运行阶段
FROM python:3.11-slim AS runtime
# 设置环境变量
ENV PYTHONUNBUFFERED=1
# 设置工作目录
WORKDIR /app
# 复制 Rust 工具链
COPY --from=builder /root/.cargo /root/.cargo
ENV PATH="/root/.cargo/bin:${PATH}"
# 复制依赖和项目文件
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin
COPY --from=builder /app .
# 安装运行时必要的系统依赖
RUN apt-get update -y && \
    apt-get install -y --no-install-recommends \
    build-essential \
    gcc \
    git \
    wget \
    ca-certificates \
    && \
    rm -rf /var/lib/apt/lists/*
# 暴露端口
EXPOSE 8020
# 运行应用
CMD ["uvicorn", "examples.lightrag_api_openai_compatible_demo:app", "--host", "0.0.0.0", "--port", "8020"]